# -*- coding: utf-8 -*-

# @Author : guoxin

# @Time : 2024/4/26 11:47

# @QQ :363673234
import selenium
import os
import time
from unittest import TestCase as TeCa

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait


def sleep_wait(poll):
    time.sleep(poll)


class AssertUtils:

    def __init__(self):
        # 浏览器驱动
        self.driver = None

    def assert_contains(self, actual, expected):
        """
        :param actual:
        :param expected:
        :return:断言@actual是否包含@expected
        """
        mes = f'实际值:[{actual}]包含预期值:[{expected}]'

        def __assert_c(act, exp_val):
            try:
                TeCa().assertIn(str(exp_val), str(act))
            except AssertionError:
                raise AssertionError(f'{mes};不包含')

        if isinstance(expected, tuple) or isinstance(expected, list):
            exp = ''
            for val in expected:
                exp = val
                if actual == val:
                    break
            __assert_c(actual, exp)
        else:
            __assert_c(actual, expected)

    def assert_contains2(self, actual, expected):
        """
        :param actual:
        :param expected:
        :return:断言@actual是否包含@expected
        """
        mes = f'实际值:[{actual}]包含预期值:[{expected}]'
        try:
            assert expected in actual
        except AssertionError:
            raise AssertionError(f'{mes};不包含')

    def assert_equals(self, actual, expected):
        """
        :param actual:
        :param expected:
        :return:断言@actual是否等于@expected
        """
        mes = f'实际值:[{actual}]等于预期值:[{expected}]'
        try:
            assert expected == actual
        except AssertionError:
            raise AssertionError(f'{mes};不包含')

    def assert_equals2(self, actual, expected):
        """
        :param actual:
        :param expected:
        :return:断言@actual是否等于@expected
        """
        mes = f'实际值:[{actual}]等于预期值:[{expected}]'
        try:
            TeCa().assertEqual(actual, expected)
        except AssertionError:
            raise AssertionError(f'{mes};不包含')

    def assert_ture(self, exp):
        """

        :param exp:
        :return: 断言是否为真
        """
        TeCa().assertTrue(exp)

    def assert_ture2(self, exp):
        """

        :param exp:
        :return: 断言是否为真
        """
        assert exp is True

    def assert_false(self, exp):
        """

        :param exp:
        :return: 断言是否为假
        """
        assert exp is False

    def assert_false2(self, exp):
        """

        :param exp:
        :return: 断言是否为假
        """
        TeCa().assertFalse(exp)

    def wait_element_visibility(self, locator, timeout=15, poll=0.3, extra_wait=None):
        """
        等待元素可见(浏览器窗口显示范围内)
        :param extra_wait: 智能等待结束后的额外等待时间/秒
        :param locator: 元素定位信息
        :param timeout: 超时时间
        :param poll: 轮询间隔时间/秒
        :return:
        """
        mes = 'xxx'
        try:
            WebDriverWait(self.driver, timeout, poll).until(ec.visibility_of_element_located(locator))
            if extra_wait:
                sleep_wait(extra_wait)
            print(f"{mes}可见")
            return True
        except Exception as e:
            print(f"{mes}不可见.{e}")
            return False

    def assert_element_visible(self, assert_locator, timeout=15):
        """断言元素是否可见"""
        assert self.wait_element_visibility(assert_locator, timeout)

    output_path = r'xxx\xxx\xxx'

    def get_current_output_dir_file_account(self):
        return FileUtils.get_file_amount_by_dir(self.output_path)


class FileUtils:
    @classmethod
    def file_if_exist(cls, file_path):
        """文件是否存在"""
        if os.path.exists(file_path) and os.path.isfile(file_path):
            return True
        return False

    @classmethod
    def get_file_amount_by_dir(cls, dir_path):
        """

        :param dir_path: 文件夹路径
        :return: 文件夹下有多少文件
        """
        gf_list = os.listdir(dir_path)
        file_list = []
        for rf in gf_list:
            file_path = os.path.join(dir_path, rf)
            if os.path.isfile(file_path):
                file_list.append(file_path)
        return len(file_list)


if __name__ == '__main__':
    # AssertUtils().assert_ture(1 > 1)
    AssertUtils().assert_contains(1, (3, 2, 2))


